package com.codeczx.providerdemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @BindView(R.id.add)
    Button add;
    @BindView(R.id.query)
    Button query;
    @BindView(R.id.update)
    Button update;
    @BindView(R.id.delete)
    Button delete;

    private String newId = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.add, R.id.query, R.id.update, R.id.delete})
    public void onViewClicked(View view) {
        Uri uri = null;

        switch (view.getId()) {
            case R.id.add:
                uri = Uri.parse("content://com.codeczx.datademo.provider/book");
                ContentValues values = new ContentValues();
                values.put("name","海贼王");
                values.put("author","尾田");
                values.put("pages",1040);
                values.put("price",22.85);
                Uri newUri = getContentResolver().insert(uri,values);
                newId = newUri.getPathSegments().get(1);
                break;
            case R.id.query:
                uri = Uri.parse("content://com.codeczx.datademo.provider/book");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if (cursor != null) {
                    while(cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.i(TAG, "onViewClicked: name"+name);
                        Log.i(TAG, "onViewClicked: author"+author);
                        Log.i(TAG, "onViewClicked: pages"+pages);
                        Log.i(TAG, "onViewClicked: price"+price);
                    }
                    cursor.close();
                }
                break;
            case R.id.update:
                Log.i(TAG, "onViewClicked: newId"+newId);
                uri = Uri.parse("content://com.codeczx.datademo.provider/book/"+newId);
                ContentValues values1 = new ContentValues();
                values1.put("name","火影");
                values1.put("author","岸本");
                values1.put("pages",1040);
                values1.put("price",22.85);
                getContentResolver().update(uri,values1,null,null);
                break;
            case R.id.delete:
                uri = Uri.parse("content://com.codeczx.datademo.provider/book");
                getContentResolver().delete(uri,null,null);
                break;
        }
    }
}
